/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ora_datadescriptor.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/21 by geyh                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "../../../../src/module/oracle/config.h"
#include <oracle/ora_core.h>
#include <oracle/comm/ora_comm.h>
#include <oracle/tns/ora_datadescriptor.h>
#include <frct_comm.h>
#include "../../test_ora_comm.h"

#include "t_ora_datadescriptor.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}


static void __case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    int i_data_len = 0;
    char *p_payload = (char *)frmemdup("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x04\x03\x02", 16);
    unsigned int ui_paylen = 16;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;

    i_ret = ora_datadescriptor_unpack(p_st_sess, p_payload, ui_paylen, &i_data_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_data_len, 84148994);

    frfree(p_payload);
    test_ora_destroy_ct(&p_st_conn);

    return;

}

static void __case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    int i_data_len = 0;
    char *p_payload = (char *)frmemdup("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16);
    unsigned int ui_paylen = 16;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;

    i_ret = ora_datadescriptor_unpack(p_st_sess, p_payload, ui_paylen, &i_data_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_data_len, 1);

    frfree(p_payload);
    test_ora_destroy_ct(&p_st_conn);

    return;

}

static void __case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    int i_data_len = 0;
    char *p_payload = (char *)frmemdup("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 12);
    unsigned int ui_paylen = 12;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;

    i_ret = ora_datadescriptor_unpack(p_st_sess, p_payload, ui_paylen, &i_data_len);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_payload);
    test_ora_destroy_ct(&p_st_conn);

    return;

}


int add_suite_ora_datadescriptor_unpack(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ora_datadescriptor_unpack()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
//    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;
}

